﻿using System;
using System.Net;
using System.Collections.Generic;

using SBPweb.Forge.Common.Patterns;

namespace SBPweb.Forge.Common.Connectivity
{

    public class CookiedWebClient : WebClient
    {

        #region Private fields

        private Guid clientId;

#if !SILVERLIGHT
        private static readonly SingletonHelper<CookieContainer> containerSingleton = new SingletonHelper<CookieContainer>();
#endif
        private Dictionary<HttpRequestHeader, string> headerValues = new Dictionary<HttpRequestHeader, string>();
        private LockHelper lhHeaderOverrides = new LockHelper("HeaderOverrides");

        private int defaultTimeOut = 45000;

        #endregion

        #region Constructor logic

        public CookiedWebClient()
            : base()
        {
            clientId = System.Guid.NewGuid();
        }

        #endregion

        #region Property accessors

        public int DefaultTimeOut
        {
            get
            {
                return defaultTimeOut;
            }
            set
            {
                defaultTimeOut = value;
            }
        }

#if !SILVERLIGHT
        public CookieContainer CookieContainer
        {
            get
            {
                //return pCC; //pContainerSingleton.Instance;
                return containerSingleton.Instance;
            }
            //set
            //{
            //    pContainerSingleton.Instance = value;
            //}
        }
#endif

        public Dictionary<HttpRequestHeader, string> HeaderOverrides
        {
            get
            {
                return headerValues;
            }
        }

        public LockHelper HeadersLockObject
        {
            get
            {
                return lhHeaderOverrides;
            }
        }

        public Guid ClientID
        {
            get { return clientId; }
            set { clientId = value; }
        }

        #endregion

        #region Methods

        #region Overridden

        protected override WebRequest GetWebRequest(Uri address)
        {
            HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);

            using (HeadersLockObject.GetLock())
            {
                foreach (KeyValuePair<HttpRequestHeader, string> kvp in headerValues)
                {
                    switch (kvp.Key)
                    {
                        case HttpRequestHeader.ContentType:
                            request.ContentType = kvp.Value;
                            break;

                        case HttpRequestHeader.ContentLength:
#if !SILVERLIGHT
                            request.ContentLength = Convert.ToInt64(kvp.Value);
#else
                            request.Headers[HttpRequestHeader.ContentLength] = kvp.Value;
#endif
                            break;

                        case HttpRequestHeader.Referer:
#if !SILVERLIGHT
                            request.Referer = kvp.Value;
#else
                            request.Headers[HttpRequestHeader.Referer] = kvp.Value;
#endif
                            break;

                        default:
                            request.Headers[kvp.Key] = kvp.Value;
                            break;
                    }
                }
            }
            if (request != null)
            {
#if !SILVERLIGHT
                request.Timeout = defaultTimeOut;
                request.CookieContainer = CookieContainer; //pContainerSingleton.Instance;
                request.KeepAlive = false;
#endif
            }
            return request;
        }

        internal WebRequest GetWebRequestInstance(Uri address)
        {
            return GetWebRequest(address);
        }

        #endregion

        #endregion

    }

}